package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {


    @Override
    public ResponseResult findChannelALl() {
        ResponseResult responseResult = ResponseResult.okResult(this.list());
        return  responseResult;
    }

    @Override
    public ResponseResult listWithPage(WmChannelDto dto) {
        try {
            log.info("展示channel列表");
            LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<WmChannel>().like(dto.getName() != null, WmChannel::getName, dto.getName())
                    .orderByDesc(WmChannel::getCreatedTime).orderByAsc(WmChannel::getId);
            Page<WmChannel> page = new Page<>(dto.getPage(), dto.getSize());
            page=this.page(page,queryWrapper);
            PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
            pageResponseResult.setData(page.getRecords());
            return pageResponseResult;
        }catch (Exception e){
            e.printStackTrace();
            return  ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }

    }
}